# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################
import hashlib  
#from makan import *

def call():
    session.forget()
    return service()

@service.xmlrpc
def add(a,b):
    
    return a+b

def index():
    """
    example action using the internationalization operator T and flash
    rendered by views/default/index.html or views/generic.html
    """
    
#    return dict(form1=auth.login(), form2=auth.register())
    response.view = "default/coming_soon.html"
    return dict()

@service.xmlrpc
def xAuth(email, password):
    result = False
    hashedPass = hashlib.md5(password).hexdigest()
    user = db(db.users.email==email).select().first()
    if user:
        if user.password == hashedPass:
            result = jsonDict(user)
        else: 
            result = "Wrong email/password"
    return result

def user():
    """
    exposes:
    http://..../[app]/default/user/login
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())


def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)


def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    session.forget()
    return service()


def play():
    f = open(r'D:\data.csv', "r")
    while True:
        line = f.readline()
        if not line: break
        line = line[0:len(line)-1].split(',')
        if len(line)==3:
            db.places.insert(name=line[2], long=line[0], lat=line[1],\
                           likes=0, dislikes=0, category= 1)
    return dict(line='done')
